import Vue from 'vue'
import App from './App.vue'
import router from './router'

//统一导入axios
import axios from 'axios'
//axios请求统一前缀
axios.defaults.baseURL = "http://localhost:8080/"
//axios起别名，$axios，每个vue文件不用再导入了，直接使用this.$axios
Vue.prototype.$axios = axios

//添加请求拦截器
axios.interceptors.request.use(function (config) {
  //在发送请求之前做些什么
  //前端localstorage有token，统一添加
  let token = window.localStorage.getItem("token");
  if (token) {
    config.headers.token = token;
  }
  return config;
}, function (error) {
  //请求错误做些什么
  return Promise.reject(error);
})

//添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  //统一处理将响应头的token存入localstorage中
  let token = response.headers.token;
  if (token) {
    window.localStorage.setItem("token", token);
  }
  if (response.data.code == 401) {
    ElementUI.Message(response.data.msg);
    router.push("/login");
  }
  return response;
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error);
});


//初始化element-ui
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

//引入echarts
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts;



Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
